﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections.Generic;
using System.Linq;
using Pipra.Math;
using Pipra.Math.Geometry;

namespace Pipra.Gis.Transformation
{
    /// <summary>
    /// A geocentric to topocentric transformation.
    /// </summary>
    public class GeocentricTopocentricTransformation : ITransformation<Point3>
    {

        protected readonly GeographicGeocentricTransformation GeographicTransform;
        private Point3 _topocentricOrigin;
        private double _sinLat;
        private double _cosLat;
        private double _sinLon;
        private double _cosLon;
        private double _sinLatCosLon;
        private double _cosLatCosLon;
        private double _cosLatSinLon;
        private double _sinLatSinLon;

        private class Inverted : InvertedTransformationBase<GeocentricTopocentricTransformation, Point3>
        {

            public Inverted(GeocentricTopocentricTransformation core) : base(core) { }

            public override Point3 TransformValue(Point3 topocentric)
            {
                return new Point3(
                    Core._topocentricOrigin.X - (topocentric.X * Core._sinLon) - (topocentric.Y * Core._sinLatCosLon) + (topocentric.Z * Core._cosLatCosLon),
                    Core._topocentricOrigin.Y + (topocentric.X * Core._cosLon) - (topocentric.Y * Core._sinLatSinLon) + (topocentric.Z * Core._cosLatSinLon),
                    Core._topocentricOrigin.Z + (topocentric.Y * Core._cosLat) + (topocentric.Z * Core._sinLat)
                );
            }

        }
        /// <summary>
        /// Creates a new geocentric to topocentric transformation.
        /// </summary>
        /// <param name="topocentricOrigin">The topocentric origin.</param>
        /// <param name="spheroid">The spheroid.</param>
        public GeocentricTopocentricTransformation(
            ICoordinateTriple<double> topocentricOrigin,
            ISpheroid<double> spheroid
        )
        {
            GeographicTransform = new GeographicGeocentricTransformation(spheroid);
            SetTopocentricOrigin(topocentricOrigin);
        }

        /// <summary>
        /// Sets the topocentric origin for this transformation.
        /// </summary>
        /// <param name="topocentricOrigin">The topocentric origin.</param>
        public void SetTopocentricOrigin(ICoordinateTriple<double> topocentricOrigin)
        {
            _topocentricOrigin = new Point3(topocentricOrigin);
            LatLon ellipsoidalOrigin =
                (GeographicTransform.GetInverse() as ITransformation<Point3, LatLon>)
                .TransformValue(_topocentricOrigin)
            ;
            _sinLat = System.Math.Sin(ellipsoidalOrigin.Lat);
            _cosLat = System.Math.Cos(ellipsoidalOrigin.Lat);
            _sinLon = System.Math.Sin(ellipsoidalOrigin.Lon);
            _cosLon = System.Math.Cos(ellipsoidalOrigin.Lon);
            _sinLatCosLon = _sinLat * _cosLon;
            _cosLatCosLon = _cosLat * _cosLon;
            _cosLatSinLon = _cosLat * _sinLon;
            _sinLatSinLon = _sinLat * _sinLon;
        }

        public Point3 TransformValue(Point3 geocentric)
        {
            Vector3 d = geocentric.Difference(_topocentricOrigin);
            return new Point3(
                (d.Y * _cosLon) - (d.X * _sinLon),
                (d.Z * _cosLat) - (d.X * _sinLatCosLon) - (d.Y * _sinLatSinLon),
                (d.X * _cosLatCosLon) + (d.Y * _cosLatSinLon) + (d.Z * _sinLat)
            );
        }

        public void TransformValues(Point3[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                TransformValue(ref values[i]);
            }
        }

        [CLSCompliant(false)]
        public void TransformValue(ref Point3 value)
        {
            Vector3 d = value.Difference(_topocentricOrigin);
            value = new Point3(
                (d.Y * _cosLon) - (d.X * _sinLon),
                (d.Z * _cosLat) - (d.X * _sinLatCosLon) - (d.Y * _sinLatSinLon),
                (d.X * _cosLatCosLon) + (d.Y * _cosLatSinLon) + (d.Z * _sinLat)
            );
        }

        public bool HasInverse
        {
            get { return true; }
        }

        public ITransformation<Point3> GetInverse()
        {
            return new Inverted(this);
        }

        ITransformation<Point3, Point3> ITransformation<Point3, Point3>.GetInverse()
        {
            return GetInverse();
        }

        ITransformation ITransformation.GetInverse()
        {
            return GetInverse();
        }

        public IEnumerable<Point3> TransformValues(IEnumerable<Point3> values)
        {
            return values.Select(TransformValue);
        }

    }
}
